package com.controller.bill;

import com.javabean.Bill;
import com.javabean.Provider;
import com.mysql.jdbc.StringUtils;
import com.service.bill.BillService;
import com.service.provider.ProviderService;
import com.service.role.RoleService;
import com.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class BillController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("/bill.do")
    public String showBillList(Model model, HttpServletRequest request) {
        String queryIsPayment = request.getParameter("queryIsPayment");
        String queryProviderName = request.getParameter("queryProviderName");
        String queryProductName = request.getParameter("queryProductName");
        if (queryProviderName == null) {
            queryProviderName = "";
        }

        if (queryProductName == null) {
            queryProductName = "";
        }

        Bill bill = new Bill();
        if (StringUtils.isNullOrEmpty(queryIsPayment)) {
            bill.setIsPayment(0);
        } else {
            bill.setIsPayment(Integer.parseInt(queryIsPayment));
        }

        List<Bill> billList = billService.getBillList(bill.getIsPayment(), queryProviderName, queryProductName);
        for (Bill bill1 : billList) {
            model.addAttribute("bill", bill1);
        }
        List<Provider> provider2List = providerService.getProvider2List();

        model.addAttribute("providerList", provider2List);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryProviderName", queryProviderName);
        model.addAttribute("queryIsPayment", queryIsPayment);
        return "billlist";
    }


    @RequestMapping("/billView")
    public String billView(Model model, HttpServletRequest request) {

        String billCode = request.getParameter("billCode");
        String billproductName = request.getParameter("billproductName");
        String billproductUnit = request.getParameter("billproductUnit");
        String billproductCount = request.getParameter("billproductCount");
        String billtotalPrice = request.getParameter("billtotalPrice");
        String billproviderName = request.getParameter("billproviderName");
        int billisPayment = Integer.parseInt(request.getParameter("billisPayment"));
        model.addAttribute("billisPayment", billisPayment);
        model.addAttribute("billproviderName", billproviderName);
        model.addAttribute("billtotalPrice", billtotalPrice);
        model.addAttribute("billproductCount", billproductCount);
        model.addAttribute("billproductUnit", billproductUnit);
        model.addAttribute("billproductName", billproductName);
        model.addAttribute("billCode", billCode);
        return "billview";
    }

    @RequestMapping("/delbill")
    public void delbill(Model model, HttpServletRequest request, HttpServletResponse response) {
        int billid = Integer.parseInt(request.getParameter("billid"));
        billService.deleteBillById(billid);
        try {
            response.sendRedirect(request.getContextPath() + "/bill.do");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
